/*
 * @Descripttion: 
 * @version: 
 * @Author: jiuguo.qin
 * @Date: 2020-01-25 19:24:32
 * @LastEditors  : jiuguo.qin
 * @LastEditTime : 2020-02-15 13:09:59
 */

#ifndef __WRK_WORKER_H__
#define __WRK_WORKER_H__

#include <unistd.h>
#include "ae.h"
#include "stdlib.h"
#include "wrk_core.h"
#include "protocol.h"

using namespace std;

class  WrkWorker
{
private:
    /* data */

    uint8_t  get_cpu_cores();

    static void read_chnnel_from_master(aeEventLoop *loop, int fd, void *data, int mask);

    // static void read_client(aeEventLoop *eventLoop, int fd, void *data, int mask);

    static void write_client(aeEventLoop *eventLoop, int fd, void *data, int mask);

    static void stop_handler(int sig);

    static void signal_exit(int sig);

    static void send_release_sig_to_master();

    static int monitor_stop_duration(aeEventLoop *loop, long long id, void *data);

    static string get_stats();

    static WrkCore *wrk_core_ptr;

    static uint32_t taskid;

    
    /* worker cmd handlers */
    static wrk_cmd_handler_t cmd_handlers[];

    static void worker_start_task_handler(int fd, wrk_channel_t ch, void *data);
    static void worker_stop_task_handler(int fd, wrk_channel_t ch, void *data);
    static void worker_stats_task_handler(int fd, wrk_channel_t ch, void *data);
    static void worker_upload_file_handler(int fd, wrk_channel_t ch, void *data);

    static void read_script_from_client(aeEventLoop *eventLoop, int fd, void *data, int mask);
    static void read_file_from_client(aeEventLoop *eventLoop, int fd, void *data, int mask);

public:
    static aeEventLoop *worker_loop;
    WrkWorker(/* args */);
    ~WrkWorker();

    void run(int fd);

    static uint32_t get_cpus_cores();

    static string get_safe_json_string(const string &str);
    

};

#endif
